package cn.pjz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.pjz.basic.result.JSONResult;

import cn.pjz.domain.Token;
import cn.pjz.domain.User;
import cn.pjz.service.ITokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 阿洲
 */

@RestController
@RequestMapping("/token")
public class TokenController {
    
    @Autowired
    public ITokenService iTokenService;

    /**
     * 
     * 生产token
     */
    @PostMapping(value="/save")
    public JSONResult save(){
        
        Token token = iTokenService.save();
        
        return JSONResult.success(token);
    }


    @PostMapping(value="/login")
    public JSONResult login(User user,Token token){

        if(BeanUtil.isEmpty(user.getId())){
            return JSONResult.error("请输入账号");
        }else if(BeanUtil.isEmpty(user.getPassword())){
            return JSONResult.error("请输入密码");
        }else if(BeanUtil.isEmpty(token.getToken())){
            return JSONResult.error("令牌丢失");
        }else if(BeanUtil.isEmpty(token.getVerificationCode())){
            return JSONResult.error("请输入验证码");

        }

        return iTokenService.login(user,token);

    }
    
}
